define(['common/js/boss-frame-angular-dev-0.1',
    'common/js/service/boss-storage-service-0.1',
    'common/js/service/order-common-service-0.1'], function () {
    diretiveModule.directive('plusGoodPrintTemplet', ['asyncDataStorage', 'MaiDialog', function () {
        return {
            scope: {
                afterLoadData: "&"
            },
            restrict: "AE",
            replace: true,
            //require: '?^ngPrint',
            transclude: true,
            templateUrl: 'common/js/html/plusGoodPrintTemplet.html',
            controller: "PrintGoodCertificateController",
            link: function (scope, element, attrs) {
                scope.$on('watch-' + attrs.goodPrintId, function (event, params) {
                    scope.printTitle = attrs.goodPrintTitle;
                    if (attrs.goodPrintType == 1) {
                        scope.printGoodMoneyTitle = "应收金额";
                        scope.moneyPerson = "收款人";
                        scope.operatPerson = "打票操作人";
                    } else if (attrs.goodPrintType == 2) {
                        scope.printGoodMoneyTitle = "应退金额";
                        scope.moneyPerson = "退款人";
                        scope.operatPerson = "退单操作人";
                    }
                    params.type = attrs.goodPrintType;
                    scope.loadGoodInfos(params);
                });
            }
        };
    }]);

    controllerModule.controller("PrintGoodCertificateController", ["MaiDialog", "$scope", "OrderCommonRestService", function (MaiDialog, $scope, OrderCommonRestService) {
        $scope.loadGoodInfos = function (param) {
            $scope.orderPrintData = [];
            OrderCommonRestService.getPrintOrderDetail(param).then(function (data) {
                if (data.goodInfos.length > 5) {
                    data.goodInfos = data.priceInfos;
                }
                data.operatMoneyName = data.orderPayUserNames;
                if (param.type == 1) {
                    data.operatPersonName = data.orderPrintUserName;
                } else {
                    data.operatPersonName = data.orderBackUserName;
                }

                if (data.goodInfos.length > 5) {
                    for (i = 0; i < data.goodInfos.length; i += 5) {
                        page = {};
                        page.customerAddr = data.customerAddr;
                        page.customerCard = data.customerCard;
                        page.customerName = data.customerName;
                        page.customerPhone = data.customerPhone;
                        page.deliverName = data.deliverName;
                        page.distributionMoney = data.distributionMoney;
                        page.eventShow = data.eventShow;
                        page.orderDiscountName = data.orderDiscountName;
                        page.orderId = data.orderId;
                        page.orderMoney = data.orderMoney;
                        page.operatMoneyName = data.orderPayUserNames;
                        page.operatPersonName = data.operatPersonName;
                        page.receivableMoney = data.receivableMoney;
                        page.remark = data.remark;
                        page.date = data.date;
                        page.time = data.time;
                        page.childChannleName = data.childChannelName;
                        page.parentChannleName = data.parentChannelName;
                        page.bottom = "<" + Math.ceil((i + 1) / 5) + "/" + Math.ceil(data.goodInfos.length / 5) + ">";
                        if (i + 5 >= data.goodInfos.length) {
                            page.goodInfos = data.goodInfos.slice(i);
                        } else {
                            page.goodInfos = data.goodInfos.slice(i, i + 5);
                        }
                        $scope.orderPrintData.push(page);
                    }
                } else {
                    $scope.orderPrintData.push(data);
                }

                console.log("打印凭证");
                console.log($scope.orderPrintData);

                setTimeout(function () {
                    var elemToPrint = document.getElementById("printdiv");
                    if (elemToPrint) {
                        printElement(elemToPrint);
                    }
                }, 100);

            }, function (message) {

            });
        }

        function printElement(elem) {
            var iframe = document.createElement('IFRAME');
            document.body.appendChild(iframe);
            var doc = iframe.contentWindow.document;
            doc.write('<div><style>@page{size:241mm 140mm;}</style>' + elem.innerHTML + '</div>');
            doc.close();
            iframe.contentWindow.focus();
            iframe.contentWindow.print();
            //打印结束清除创建的iframe
            document.body.removeChild(iframe);
            $scope.afterLoadData();
        }
    }]);
});